$(function () {
    var locationClass_datagrid = $("#locationClass_datagrid");
    var loclz_dialog = $("#loclz_dialog");
    var loclz_form = $("#loclz_form");
    locationClass_datagrid.datagrid({
        url: '/locationClass/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '教室名称', width: 100},
            {field: 'addr', title: '教室地址', width: 100},
            {field: 'seating', title: '座位数', width: 100},
            {
                field: 'status', title: '教室状态', width: 100, formatter: function (value) {
                return value ? "在用" : "<font color='red'>停用</font>"
            }
            },
            {field: 'slogan', title: '教室标语', width: 100},
        ]],
        singleSelect: true,
        pagination: true,
        ortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: "#loclz_btns",
        onClickRow: function (index, row) {
            if (row.state) {
                $("#changeState_btn").linkbutton({
                    text: '在用'
                })
            } else {
                $("#changeState_btn").linkbutton({
                    text: '停用'
                })
            }
        }
    });
    loclz_dialog.dialog({
        width: 300,
        height: 400,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            loclz_form.form('clear');
        }
    });
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    })
    var methodObject = {
        add: function () {
            loclz_dialog.dialog('setTitle', '新增教室');
            loclz_dialog.dialog('open');
        },

        edit: function () {
            var row = locationClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            loclz_form.form('load', row);
            loclz_dialog.dialog('setTitle', '编辑教室');
            loclz_dialog.dialog('open');
        },
        changeState: function () {
            var row = locationClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/locationClass/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            locationClass_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        del: function () {
            var row = locationClass_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该班级吗？', function (r) {
                if (r) {
                    $.get("/locationClass/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            locationClass_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            locationClass_datagrid.datagrid('reload');
        },

        save: function () {
            loclz_form.form('submit', {
                url: '/locationClass/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObject.cancel();
                        locationClass_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            loclz_dialog.dialog('close');
        },
        query: function () {
            var keyword = $("[name=keyword]").val();
            locationClass_datagrid.datagrid('load', {
                keyword: keyword
            })
        }
    }
})


